package com.zhixiang.core.util;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.security.Authentication;
import org.springframework.security.context.SecurityContext;
import org.springframework.security.context.SecurityContextHolder;

import com.zhixiang.xaec.entity.system.AppUser;

public class ContextUtil
{
  private static final Log logger = LogFactory.getLog(ContextUtil.class);

  public static AppUser getCurrentUser()
  {
    SecurityContext localSecurityContext = SecurityContextHolder.getContext();
    if (localSecurityContext != null)
    {
      Authentication localAuthentication = localSecurityContext.getAuthentication();
      if (localAuthentication != null)
      {
        Object localObject = localAuthentication.getPrincipal();
        if ((localObject instanceof AppUser))
          return (AppUser)localObject;
      }
      else
      {
        logger.warn("WARN: securityContext cannot be lookuped using SecurityContextHolder.");
      }
    }
    return null;
  }

  public static Long getCurrentUserId()
  {
    AppUser localAppUser = getCurrentUser();
    if (localAppUser != null){
    	return localAppUser.getUserId();
    }
    return null;
  }
}

